#!/bin/sh
#
# Copyright (C) 2015 NatShell.com
#


NATSHELL_OPTIONS_CHANGED=0

[ ! -e /etc/config/nsapd ]  && [ ! -e /etc/config/natshell ] && exit 0

[ -e /etc/config/initdone  ] && exit 0

. /lib/functions/system.sh
. /lib/functions/uci-defaults.sh
. /lib/functions.sh
. /lib/ar71xx.sh


### @brief 更新natshell的net_if section
### @param $1 为section name
###	@param $@ 为相关参数 <mode> <input=name> <output=name>
###				ep: "route" "input=eth0" "output=br-lan"
do_net_if_update_aptunnel(){
	local sname=$1; shift
	local mode=$1; shift
	local tuples="$@"
	
	if [ "$sname" == "input"  ] ; then
		uci set natshell.${sname}.mode=${mode}
		NATSHELL_OPTIONS_CHANGED=1
	fi

	### 遍历参数组，获得对象
	for tuple in  $tuples; do
		local intf_key=${tuple%=*}
		local intf_val=${tuple#*=}
		### 判断当前循环的name，然后进行设置
		if [ "$sname" == "$intf_key"  ] ; then
			##config_set $sname name $intf_val
			uci set natshell.${intf_key}.name=${intf_val}
			NATSHELL_OPTIONS_CHANGED=1
		fi
	done;
}

do_net_if_update_nsapd(){
	local sname=$1; shift
	local mode=$1; shift
	local tuples="$@"

	uci set nsapd.${sname}.mode="all"
	NATSHELL_OPTIONS_CHANGED=1

	### 遍历参数组，获得对象
	for tuple in  $tuples; do
		local intf_key=${tuple%=*}
		local intf_val=${tuple#*=}
		### 判断当前循环的name，然后进行设置
		if [ "input" == "$intf_key"  ] ; then
			uci set nsapd.${sname}.wan=${intf_val}
			NATSHELL_OPTIONS_CHANGED=1
		fi
		if [ "output" == "$intf_key"  ] ; then
			uci set nsapd.${sname}.lan=${intf_val}
			NATSHELL_OPTIONS_CHANGED=1
		fi
	done;
}

get_ether_macaddr(){
	local ether=$1
	local macaddr=`cat /sys/class/net/${ether}/address  | awk -F : '{print $1$2$3$4$5$6}' | tr '[a-z]' '[A-Z]'`
	echo $macaddr
}

update_our_system_hostname() {
	uci set system.@system[0].hostname=$1
}
update_our_network_hostname(){
	uci set network.lan.hostname=$1
}

### 生成我们的network
### @param $1 wan_intf in br
### @param $2 lan_intf in br
generate_our_network_lan() {
	local fip=${2:-"192.168.1.1"}
	local fmask=${3:-"255.255.255.0"}
        uci -q batch <<EOF
delete network.wan6
delete network.lan
set network.lan=interface
set network.lan.force_link='1'
set network.lan.type='bridge'
set network.lan.proto='dhcp'
set network.lan.hostname=`apid | awk -F - '{print $5}'`
set network.lan.ifname='$1'
set network.lan.failover_ipaddr='$fip'
set network.lan.failover_netmask='$fmask'
EOF
}

### @param $1 ifname in bridge
### @param $2 ipaddr 
### @param $3 netmask
generate_our_network_lan_static() {
        uci -q batch <<EOF
delete network.wan6
delete network.lan
set network.lan=interface
set network.lan.force_link='1'
set network.lan.type='bridge'
set network.lan.proto='static'
set network.lan.ifname='$1'
set network.lan.ipaddr='$2'
set network.lan.netmask='$3'
EOF
}

### @param $1 wan_intf
### @param $2 lan_intf
generate_our_network_wan() {
        uci -q batch <<EOF
delete network.wan6
delete network.wan
set network.wan='interface'
set network.wan.ifname='$1'
#set network.loopback.proto='dhcp'
EOF
}

generate_our_network_wan_bridge() {
        uci -q batch <<EOF
delete network.wan6
delete network.wan
set network.wan='interface'
set network.wan.force_link='1'
set network.wan.type='bridge'
set network.wan.ifname='$1'
set network.wan.proto='dhcp'
set network.wan.hostname='`apid | awk -F - '{print $5}'`'
EOF
}

generate_our_network_wan_static() {
        uci -q batch <<EOF
delete network.wan
delete network.wan
set network.wan=interface
set network.wan.force_link='1'
set network.wan.proto='static'
set network.wan.ifname='$1'
set network.wan.ipaddr='$2'
set network.wan.netmask='$3'
EOF
}

delete_our_network_wan() {
        uci -q batch <<EOF
delete network.wan6
delete network.wan
EOF
}

open_our_dhcp_server(){
	uci set dhcp.lan.ignore=0
	uci commit dhcp
}

close_our_dhcp_server(){
	uci set dhcp.lan.ignore=1
	uci commit dhcp
}

update_aptunnel(){
	if [[ -e /etc/config/natshell ]]; then
		config_load natshell
		config_foreach do_net_if_update_aptunnel net_if $@
	fi
	if [[ -e /etc/config/nsapd ]]; then
		config_load nsapd
		config_foreach do_net_if_update_nsapd net_if $@
	fi
}

### @param $1 ac_ipaddr
update_aptunnel_acip(){
	if [[ -e /etc/config/natshell ]]; then
		uci set natshell.AC.ac_ipaddr=$1
		NATSHELL_OPTIONS_CHANGED=1
	fi
	if [[ -e /etc/config/nsapd ]]; then
		uci set nsapd.ac_host.host=$1
		NATSHELL_OPTIONS_CHANGED=1
	fi
}

## @param $1 nat(1) or route(0), $2 thin or fat
update_apmode(){
	if [[ -e /etc/config/apmode ]]; then
		if [[ "$1" == "nat" ]]; then
			uci set apmode.apmode.nat=1
			NATSHELL_OPTIONS_CHANGED=1
		elif [[ "$1" == "route" ]]; then
			uci set apmode.apmode.nat=0
			NATSHELL_OPTIONS_CHANGED=1
		fi
		uci set apmode.apmode.mode=$2
		NATSHELL_OPTIONS_CHANGED=1
		
	fi
}

board=$(ar71xx_board_name)
mac=""

case "$board" in
	mesh-5-x1 | \
	mesh-5-x2)
		update_aptunnel "bridge" "input=eth1" "output=br-lan"
		generate_our_network_lan "eth1"
		;;
	mh-ap9341)
		update_aptunnel "bridge" "input=eth0" "output=br-wan"
		generate_our_network_lan_static "eth1" "192.168.2.100" "255.255.255.0"
		generate_our_network_wan_bridge "eth0"
		;;
	e600g)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan "eth0 eth1"
		delete_our_network_wan
		close_our_dhcp_server
		;;
	cf-7-e350n)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		;;
	cf-7-e325n)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan "eth0"
		;;
	cf-7-e355ac)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		;;
	cpe860 |\
	fwbd-1805 | \
	ns-a160 | \
	bd-xd9531 | \
	bd-xd6700 | \
	ns-b410 | \
	ns-a310 | \
	ns-a210 | \
	ns-hdt-302p |\
	ns-301p |\
	ns-8-pw3)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan "eth0 eth1"
		delete_our_network_wan
		close_our_dhcp_server
		;;
	p161 )
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan "eth0 eth1"
		delete_our_network_wan
		close_our_dhcp_server
		;;
	#mb1h )
	#	update_aptunnel "bridge" "input=eth0" "output=br-lan"
	#	generate_our_network_lan_static "eth1" "192.168.0.1" "255.255.255.224"
	#	generate_our_network_wan_static "eth0" "192.168.66.66" "255.255.255.0"
	#	mac=$(get_ether_macaddr "eth0")
	#	update_our_system_hostname "$mac"
	#	update_our_network_hostname "$mac"
	#	;;
	ns-b460 | \
	ns-g460)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan "eth0"
		delete_our_network_wan
		close_our_dhcp_server
		;;
	ns-8-xd3200)
		update_aptunnel "bridge" "input=eth0.2" "output=br-lan"
		;;
	lw-c2)
		update_aptunnel "bridge" "input=eth0.2" "output=br-lan"
		;;
	fby-143)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan_static "eth1" "192.168.1.1" "255.255.255.0"
		generate_our_network_wan_static "eth0" "192.168.66.66" "255.255.255.0"
		;;
	fwbd-1807 |\
	ns-180p)
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan "eth0"
		delete_our_network_wan
		close_our_dhcp_server
		;;
	ns-180p-debug)
		update_aptunnel "bridge" "input=eth0.2" "output=br-lan"
		;;
	### only one intf
	xd9318 | \
	fwbd-1907 | \
	hb3002 | \
	ns-g-iwn600 | \
	ns-210bgn | \
	aw-b1n | \
	shx-6-ap10 )
		update_aptunnel "bridge" "input=eth0" "output=br-lan"
		generate_our_network_lan "eth0"
		delete_our_network_wan
		close_our_dhcp_server
		;;
	*)
		;;
esac

### update ac ipaddr
case "$board" in
	mh-ap9341)
		update_aptunnel_acip "0.0.0.0"
		;;
	#p161 | \
	#mb1h )
	#	update_apmode "route" "fat"
	#	;;
	*)
		;;
esac

[ "$NATSHELL_OPTIONS_CHANGED" = "1" ] && [ -e /etc/config/natshell  ]   && uci commit natshell
[ "$NATSHELL_OPTIONS_CHANGED" = "1" ] && [ -e /etc/config/nsapd  ]   && uci commit nsapd

touch /etc/config/initdone
uci commit system
uci commit network



exit 0
